using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Presentation.Contracts;
using Presentation;
using Globals;
using DataAccess;

namespace ProjektZespolowy
{
    /// <summary>
    /// Master page.
    /// </summary>
    public partial class MasterPage : System.Web.UI.MasterPage, IMasterPageView
    {
        #region Members

        /// <summary>
        /// Instance of presenter.
        /// </summary>
        private MasterPagePresenter presenter;

        #endregion Members

        #region Override Methods

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init"></see> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"></see> object that contains the event data.</param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            presenter = new MasterPagePresenter(this);
        }

        #endregion Override Methods

        #region Private Methods

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(searchfield.Text))
            {
                presenter.Initialize();
            }
            
            //check if user is logged
            if (Session[ContentNamesGlobals.SessionWithUserId] != null)
            {
                //phLogged.Visible = true;
                //menuPlaceHolder.Visible = true;
                //menuPanel.Visible = true;
                //pnlLogged.Visible = true;
                //pnlLogin.Visible = false;
                //accountPlaceHolder.Visible = false;

                phLogged.Visible = true;

                lph.Visible = false;
                aph.Visible = true;
                fph.Visible = true;
                frph.Visible = true;
                gph.Visible = true;

                presenter.SetLoggedUser();
                presenter.FillUserGalleries();
                presenter.PutNumOfMsg();
                presenter.PutInvMum();
                presenter.FillUserFriends();
            }
            else
            {
                //phLogged.Visible = false;
                //menuPlaceHolder.Visible = false;
                
                //menuPanel.Visible = false;
                //pnlLogged.Visible = false;
                //accountPlaceHolder.Visible = true;
                //pnlLogin.Visible = true;

                phLogged.Visible = false;

                lph.Visible = true;
                aph.Visible = false;
                fph.Visible = false;
                frph.Visible = false;
                gph.Visible = false;
            }

		if (!IsPostBack)
			{
			
			}

        }

        /// <summary>
        /// Called when [BTN search click].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void OnBtnSearchClick(object sender, EventArgs e)
        {
		if ((tagCheckBox.Checked || userCheckBox.Checked || artCheckBox.Checked || commentCheckBox.Checked) && (searchfield.Text.Length>0 && !string.IsNullOrEmpty(searchfield.Text)))
			{
			Response.Redirect("~/SearchPage.aspx?Tag=" + tagCheckBox.Checked.ToString() + "&User=" + userCheckBox.Checked.ToString() + "&Art=" + artCheckBox.Checked.ToString() + "&Comment=" + commentCheckBox.Checked.ToString() + "&Phrase=" + searchfield.Text);
			}
			
			//presenter.Search();
        }

        /// <summary>
        /// Handles the Click event of the btnLogin control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            presenter.LoginUser();
            Response.Redirect("~/Profile/ShowProfile.aspx");
        }

        /// <summary>
        /// Handles the Click event of the btnLogout control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnLogout_Click(object sender, EventArgs e)
        {
            Session[ContentNamesGlobals.SessionWithUserId] = null;
            Response.Redirect("~/Default.aspx");
        }

        #endregion Private Methods       
    
        #region IMasterPageView Members

        /// <summary>
        /// Gets or sets the search text.
        /// </summary>
        /// <value>The search text.</value>
        public string SearchText
        {
            get
            {
                return searchfield.Text;
            }
            set
            {
                searchfield.Text = value;
            }
        }

        /// <summary>
        /// Gets or sets the login.
        /// </summary>
        /// <value>The login.</value>
        public string Login
        {
            get
            {
                return txtLogin.Text;
            }
            set
            {
                txtLogin.Text = value;
            }
        }

        /// <summary>
        /// Gets or sets the password.
        /// </summary>
        /// <value>The password.</value>
        public string Password
        {
            get
            {
                return txtPassword.Text;
            }
            set
            {
                txtPassword.Text = value;
            }
        }

        /// <summary>
        /// Gets or sets the user id.
        /// </summary>
        /// <value>The user id.</value>
        public string UserId
        {
            get
            {
                if (Session[ContentNamesGlobals.SessionWithUserId] != null)
                {
                    return Session[ContentNamesGlobals.SessionWithUserId].ToString();                    
                }
                else
                {
                    return "0";
                }
            }
            set
            {
                Session[ContentNamesGlobals.SessionWithUserId] = value;
            }
        }

        public List<forum> Forums
        {
            get
            {
                return (List<forum>)forumsReperater.DataSource;
            }

            set
            {
                forumsReperater.DataSource = value;
                forumsReperater.DataBind();
            }
        }

        public string NumMsg
        {
            get { return numMsg.Text; }
            set { numMsg.Text = value; }
        }

        public string Invitations
        {
            set { inv.Text = "(" + value + ")"; }
        }

        public IList<user> Friends
        {
            get { return (IList<user>)friendsReperater.DataSource; }
            set { 
                friendsReperater.DataSource = value;
                friendsReperater.DataBind();
            }
        }
	
	

        /// <summary>
        /// Sets the login message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void SetLoginMessage(string message)
        {
            lblLoginMessage.Visible = true;
            lblLoginMessage.Text = message;
        }

        /// <summary>
        /// Sets the logged user.
        /// </summary>
        /// <param name="login">The login.</param>
        public void SetLoggedUser(string login)
        {
            lblLoggedAs.Text = login;
        }

        /// <summary>
        /// Redirects to page.
        /// </summary>
        /// <param name="iPage">The i page.</param>
        public void RedirectToPage(string iPage)
        {
            Response.Redirect(iPage);
        }

        #endregion IMasterPageView Members


		private void expandSelectedNodes(TreeNode node, string url)
			{
			if (node.NavigateUrl == url) { node.Expand(); if(node.Parent!=null)node.Parent.Expand(); }
	
			
			if (node.ChildNodes.Count > 0)
				for (int i = 0; i < node.ChildNodes.Count; i++)
					expandSelectedNodes(node.ChildNodes[i], url);
			
			
			return;
			}

        //protected void TreeView1_DataBound(object sender, EventArgs e)
        //    {
        //    for (int i = 0; i < TreeView1.Nodes.Count; i++)
        //        {
        //        expandSelectedNodes(TreeView1.Nodes[i], Request.RawUrl);
        //        }
        //    }

        public void DisplayGalleries(List<gallery> galleries)
        {
            repGalleries.DataSource = galleries;
            repGalleries.DataBind();
        }

		public void ShowForums(DataTable forums)
			{
			forumsReperater.DataSource = forums;
			forumsReperater.DataBind();
			}

        public string GetAvatar(user u) {
            
            return u.avatar_mime == null ? "/images/blank-photo.gif" : "/GetUserPicture.aspx?id=" +u.id.ToString();

        }
    }
}
